//
//  NWLazyProperty.h
//  noworry
//
//  Created by dl on 2017/10/15.
//  Copyright © 2017年 dl. All rights reserved.
//

#ifndef NWLazyProperty_h
#define NWLazyProperty_h

/* 业务开发纯代码布局，经常要写大量的getter方法，用宏替代重复代码的成本
 ps:
 1.xcode8有source editor extentions,但配置起来成本较高 http://www.jianshu.com/p/030b46492d5a
 2.codesnip使用成本也高，需要手动替换多个
 -(<#(clsname)#> *)<#(varname)#>
 {
 if (_<#(varname)#> == nil) {
 _<#(varname)#> = [<#(clsname)#> new];
 }
 return _<#(varname)#>;
 }
 */

#ifndef NWLazyProperty
#define NWLazyProperty(cls,var) -(cls *)var{if (_##var == nil) {_##var = [cls new];}return _##var;}
#endif

#ifndef NWLazyPropertyWithInit
#define NWLazyPropertyWithInit(cls,var,code) -(cls *)var{if (_##var == nil) {_##var = [cls new];{code}}return _##var;}
#endif

/* 用法
 
 // 不用设置
 NWLazyProperty(NSDictionary, dict);
 
 // 普通对象
 NWLazyPropertyWithInit(NSMutableArray, mutableArray, {
    [_mutableArray addObject:@"1"];
 });
 
 // UI控件
 NWLazyPropertyWithInit(UILabel, titleLabel, {
    _titleLabel.text = @"abc";
    _titleLabel.font = [UIFont systemFontOfSize:18];
 });
 
 */

#endif /* NWLazyProperty_h */
